<?php
/**
 * User: Allen
 * Date: 15-10-14
 * 车辆列表控制器
 */

class Vehicle extends PC_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('vehicle_model');
		$this->load->model('user_model');
		$this->load->model('company_model');
	}
	
	/**
	 * @brief 车辆列表页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */	
	public function vehicle_list() {
		$this->load->view('/www/vehicle_list.php',$this->data);
	}
	
	/**
	 * @brief ajxa获取车辆列表信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function ajax_vehicle_list() {
		$data = $_GET;
		$start = $data['iDisplayStart']; //显示的起始索引
		$length = $data['iDisplayLength'];//显示的行数
		$sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列 
		$sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
		$search = $data ['sSearch'];//全局搜索字段
		$where['vehicle.status'] = 1;
		$where['concat(vehicle.id) like'] = '%'.trim($search).'%';
		$this->data['count'] = $this->vehicle_model->getCount($where);
		$aaData = array();
		if($this->data['count']) {
			$order_by ="vehicle.". $sort_th." ".$sort_type;
			$vehicle_list = $this->vehicle_model->getVehicleList($where,$length,$start,$order_by);
			$this->load->helper('string_helper');
			foreach($vehicle_list as $key => $value) {
				if($value['remark']) {
					$value['intro'] = cn_substr_utf8($value['remark'],70);
					if($value['intro'] == $value['remark']) {
						$value['intro'] = $value['remark'];
					}else {
						$value['intro'] = $value['intro']."...";
					}
				}else {
					$value['intro'] = '';
				}
				if($value['card_image']) {
					$vehicle_list[$key]['card_image'] = '<img src="'.$value['card_image'].'" style="height:30px;">';
				}else {
					$vehicle_list[$key]['card_image'] = '<img src="/public_source/wap/upload/default.jpg" style="height:30px;">';
				}
				if($value['licence_image']) {
					$vehicle_list[$key]['licence_image'] = '<img src="'.$value['licence_image'].'" style="height:30px;">';
				}else {
					$vehicle_list[$key]['licence_image'] = '<img src="/public_source/wap/upload/default.jpg" style="height:30px;">';
				}
				$vehicle_list[$key]['offer_status'] = '已报价';
				if($value['offer_status'] == 1) {
					$vehicle_list[$key]['offer_status'] = '未报价';
				}				
				$vehicle_list[$key]['remark'] = '<div class="intro" style="position:relative;cursor:pointer;">'.$value['intro'].'<div class="ser_intro">'.$value['remark'].'<i></i></div></div>';
				$del_oper = "onclick=\"if(confirm('确实要删除此条记录吗？')) return true;else return false; \"";
				$edit_url = '<a href="/www/vehicle/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
				$del_url = '<a href="/www/vehicle/del/'.$value['id'].'"  '.$del_oper.'><span class="btn btn-xs btn-danger cancel">删除</span></a>';
				$vehicle_list[$key]['operate'] = $edit_url." ".$del_url;
			}
			$aaData = $vehicle_list;
	
		}
		$output['aaData'] = $aaData;
		$output['sEcho'] = $_GET['sEcho'];
		$output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
		$output['iTotalRecords'] = $this->data['count']; //总共有几条数据
		echo json_encode($output); //最后把数据以json格式返回
	}
	
	/**
	 * @brief 进入编辑页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function edit_page() {
		$id = $this->uri->segment(4);
		$where['vehicle.id'] = $id;
		$vehicle_info = $this->vehicle_model->checkVehicle($where);
		$addr = unserialize($vehicle_info['areas']);
		$vehicle_info['addr'] = $addr['detail'];
		if($vehicle_info['card_image']) {
			$vehicle_info['card_image'] = '<img src="'.$vehicle_info['card_image'].'" style="max-height:200px;">';
		}else {
			$vehicle_info['card_image'] = '<img src="/public_source/wap/upload/default.jpg" style="max-height:200px;">';
		}
		if($vehicle_info['licence_image']) {
			$vehicle_info['licence_image'] = '<img src="'.$vehicle_info['licence_image'].'" style="max-height:200px;">';
		}else {
			$vehicle_info['licence_image'] = '<img src="/public_source/wap/upload/default.jpg" style="max-height:200px;">';
		}
		$this->data['data'] = $vehicle_info;
		$this->load->view('/www/vehicle_edit',$this->data);
	}
			
	/**
	 * @brief 保存编辑信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function edit() {
		$data = $_POST;
		$data['update_time'] = time();
		$where['id'] = $data['id'];
		$edit_result = $this->vehicle_model->editVehicle($where,$data);
		$go_url = '/www/vehicle/vehicle_list';
		if($edit_result) {
			$this->location_href($this->lang->line('edit_success'),$go_url);	
		}else {
			$this->location_href($this->lang->line('edit_false'),$go_url);	
		}
	}
	
	/**
	 * @brief 检查保险险种名唯一性
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function check_sole() {
		$data = $_POST;
		if(isset($data['id'])) {
			$sole_where['id !='] = $data['id'];
		}
		$sole_where['name'] = $data['name'];
		$sole_where['status'] = 1;
		$sole = $this->insurance_model->checkInsurance($sole_where);
		if($sole) {
			$message = false;
		}else {
			$message = true;
		}
		echo json_encode($message);
	}
	
	/**
	 * @brief 删除车辆信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function del() {
		$id = $this->uri->segment(4);
		$data['update_time'] = time();
		$data['status'] = 0;
		$where['id'] = $id;
		$del_result = $this->vehicle_model->editVehicle($where,$data);
		$go_url = '/www/vehicle/vehicle_list';
		if($del_result) {
			$this->location_href($this->lang->line('del_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('del_false'),$go_url);
		}
	}

	/**
	 * @brief 进入添加页面
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function add_page() {
		$id = $this->uri->segment(4);
		$where['id'] = $id;
		$user_info = $this->user_model->checkUser($where);
		$company_where['status'] = 1;
		$company_list = $this->company_model->getInsureAll($company_where);
		$addr = unserialize($user_info['areas']);
		$user_info['addr'] = $addr['detail'];
		$this->data['user_info'] = $user_info;
		$this->data['company_list'] = $company_list;
		$this->load->view('/www/vehicle_add',$this->data);
	}
	
	/**
	 * @brief 保存添加信息
	 * @param Null
	 * @par 2015/10/14 Ver 1.00 Created by Allen
	 */
	public function add() {
		$data = $_POST;
		$data['create_time'] = $data['update_time'] = time();
		$add_result = $this->vehicle_model->addVehicle($data);
		$go_url = '/www/vehicle/vehicle_list';
		if($add_result) {
			$this->location_href($this->lang->line('add_success'),$go_url);
		}else {
			$this->location_href($this->lang->line('add_false'),$go_url);
		}
	}
}
?>